home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1998 July / EnigmA AMIGA RUN 29 (1998)(G.R. Edizioni)(IT)[!][issue 1998-07 & 08].iso / earcd / grafica / video easel / scripts / importxlife.rexx < prev    next >
OS/2 REXX Batch file  |  1996-11-22  |  18KB  |  503 lines

  1. /*************************************************************************
  2.  **                                                                     **
  3.  ** Import XLife into VideoEasel... ARexx Script using VideoEasel-Port  **
  4.  ** Version 2.00 - 05 Nov 1996   © THOR-Software                        **
  5.  **                                                                     **
  6.  *************************************************************************/
  7.  
  8. 'LOCKGUI'
  9. 'REQUESTFILE' 'XLife/' '"XLife Source"' 'FALSE' 'source'
  10. if source~='' then; do
  11.   success=ImportXLife(source)
  12. end; else
  13.    success=1
  14.  
  15. 'UNLOCKGUI'
  16.  
  17. if success then
  18.       exit 0
  19. else  exit 20
  20.  
  21. /*************************************************
  22.  **     PlotPixel: Set point at (x,y)           **
  23.  **     Check range                             **
  24.  *************************************************/
  25. PlotPixel:     Procedure
  26.    parse arg x,y
  27.    'INSIDE' x y 'in'
  28.    if in='TRUE' then
  29.       'CLICK' x y 'LEFT'
  30.    else; do
  31.       Call PostError("The brush is too big !")
  32.       return 0
  33.    end
  34. return 1
  35.  
  36. /*************************************************
  37.  ** PostError(failstring)                       **
  38.  ** Display a requester                         **
  39.  *************************************************/
  40. PostError:     Procedure
  41.    parse arg failstring
  42.    'REQUESTFAULT' '"' || failstring || '"'
  43. return
  44.  
  45. /*************************************************
  46.  ** ReadLine                                    **
  47.  ** read a line from the given file             **
  48.  *************************************************/
  49. ReadLine:      Procedure
  50.    parse arg file
  51.    do until (line~='' & left(line,2)~='#O' & left(line,2)~='#C' & left(line,2)~='#N') | eof(file)
  52.       line=readln(file)
  53.       line=space(line,1)
  54.    end
  55.    if eof(file) then
  56.       line='#^'         /* generate EOF condition */
  57. return line
  58.  
  59. /*************************************************
  60.  ** ParseInclude                                **
  61.  ** parse a include command                     **
  62.  *************************************************/
  63. ParseInclude:   Procedure       expose x_min y_min x_max y_max pass xoffset yoffset filecount
  64.         parse arg line myname
  65.  
  66.         line=substr(line,3)
  67.  
  68.         name=word(line,1)
  69.         xo=word(line,2)
  70.         yo=word(line,3)         /* Get offsets, if present */
  71.         rot=word(line,4)        /* Get rotation */
  72.         flip=word(line,5)       /* Get flip */
  73.         del=word(line,6)        /* Get delay */
  74.  
  75.  
  76.         if xoffset='' then
  77.                 xo=0
  78.  
  79.         if yoffset='' then
  80.                 yo=0
  81.  
  82.         if (~datatype(xo,'W') | ~datatype(yo,'W')) then; do
  83.                 Call PostError('Found invalid offsets in include.')
  84.                 return '#!';
  85.         end;
  86.  
  87.         if rot='' then
  88.                 rot=0
  89.  
  90.         if flip='' then
  91.                 flip=0
  92.  
  93.         if del='' then
  94.                 del=0
  95.  
  96.         if (~datatype(rot,'W') | ~datatype(flip,'W') | ~datatype(del,'W')) then; do
  97.                 Call PostError('Found invalid parameters in include.')
  98.                 return '#!';
  99.         end;
  100.  
  101.         ppos=pos(':',name)
  102.         if ppos=0 then
  103.                 pattern=''
  104.         else; do
  105.                 pattern=substr(name,ppos+1)
  106.                 if ppos>0 then
  107.                         name=left(name,ppos-1)
  108.                 else    name=myname
  109.         end
  110.  
  111.         if ParseFile(name,pass,xo+xoffset,yo+yoffset) then
  112.                 return '#C'
  113.  
  114. return '#!'
  115.  
  116. /*************************************************
  117.  ** ParseRunLengthLine                          **
  118.  ** parse lines of a RLE pattern                **
  119.  *************************************************/
  120. ParseRunLengthLine:  Procedure      expose x_min y_min x_max y_max pass
  121.         parse arg file,line,xoffset,yoffset
  122.  
  123.         ypos=yoffset
  124.         if ypos<y_min then
  125.                 y_min=ypos
  126.         xpos=xoffset
  127.         if xpos<x_min then
  128.                 x_min=xpos
  129.         rc=1                            /* number of repeats */
  130.  
  131.         do while left(line,1)~='#'      /* until new command */
  132.                 line=compress(line)     /* remove all spaces */
  133.                 do i=1 to length(line)
  134.                         mc=substr(line,i)
  135.                         vd=verify(mc,'0123456789')
  136.                         if vd~=1 then; do
  137.                                 if vd=0 then
  138.                                         rc=mc
  139.                                 else    rc=left(mc,vd-1)
  140.                                 i=i+length(rc)-1
  141.                         end; else; do
  142.                                 mc=left(mc,1)
  143.                                 if mc='!' then; do
  144.                                         if ypos>y_max then
  145.                                                 y_max=ypos
  146.  
  147.                                         return '#C'     /* return comment line, flush this line */
  148.                                 end
  149.                                 if mc='$' then; do
  150.                                         if ypos>y_max then
  151.                                                 y_max=ypos
  152.  
  153.                                         xpos=xoffset    /* next line */
  154.                                         ypos=ypos+rc
  155.                                         rc=1
  156.                                 end; else; do
  157.                                         if pass=0 then; do
  158.                                                 xpos=xpos+rc-1
  159.                                                 if xpos>x_max then
  160.                                                         x_max=xpos
  161.  
  162.                                                 xpos=xpos+1
  163.                                         end; else; do
  164.                                                 if mc='b' then
  165.                                                         xpos=xpos+rc
  166.                                                 else; do
  167.                                                         if mc~='o' then; do
  168.                                                                 Call PostError('Found illegal RLE entry.')
  169.                                                                 return '#!'
  170.                                                         end
  171.  
  172.                                                         do k=1 to rc
  173.                                                                 if ~PlotPixel(xpos-x_min,ypos-y_min) then
  174.                                                                         return '#!'
  175.  
  176.                                                                 xpos=xpos+1
  177.                                                         end
  178.                                                 end     /* of if not background */
  179.                                         end             /* of pass=1 */
  180.                                         rc=1            /* reset run length to default */
  181.                                 end     /* of no new line */
  182.                         end /* of no run length control */
  183.                 end /* of for characters in command line */
  184.  
  185.                 line=ReadLine(file)
  186.         end /* of valid line found */
  187.  
  188. return line
  189.  
  190. /*************************************************
  191.  ** ParsePictureLine                            **
  192.  ** parse lines of a picture file               **
  193.  *************************************************/
  194. ParsePictureLine:  Procedure       expose x_min y_min x_max y_max pass
  195.         parse arg file,line,xoffset,yoffset
  196.  
  197.         ypos=yoffset
  198.         if ypos<y_min then
  199.                 y_min=ypos
  200.  
  201.         do while left(line,1)~='#'       /* until new command */
  202.                 line=strip(line)
  203.                 xpos=xoffset
  204.                 if pass=0 then; do
  205.                         if xpos<x_min then
  206.                                 x_min=xpos
  207.                         xpos=xpos+length(line)-1
  208.                         if xpos>x_max then
  209.                                 x_max=xpos
  210.  
  211.                 end; else; do
  212.                         do i=1 to length(line)
  213.                                 mc=substr(line,i,1)
  214.                                 if mc='*' | mc='+' then
  215.                                         if ~PlotPixel(xpos-x_min,ypos-y_min) then
  216.                                                 return '#!'
  217.                                 xpos=xpos+1
  218.                         end
  219.                 end
  220.                 if ypos>y_max then
  221.                         y_max=ypos
  222.                 ypos=ypos+1
  223.                 line=ReadLine(file)
  224.         end
  225.  
  226. return line
  227.  
  228. /*************************************************
  229.  ** ParseListLine                               **
  230.  ** parse lines of a coordinate list            **
  231.  *************************************************/
  232. ParseListLine:  Procedure       expose x_min y_min x_max y_max pass
  233.         parse arg file,line,xoffset,yoffset
  234.  
  235.  
  236.         do while left(line,1)~='#'       /* until new command */
  237.                 xpos=word(line,1)
  238.                 ypos=word(line,2)
  239.  
  240.                 if xpos='' | ypos='' | ~datatype(xpos,W) | ~datatype(ypos,W) then; do
  241.                         Call PostError('Found illegal coordinate entry.')
  242.                         return '#!'
  243.                 end
  244.  
  245.                 xpos=xpos+xoffset
  246.                 ypos=ypos+yoffset
  247.                 if pass=0 then; do
  248.                         if xpos<x_min then
  249.                                 x_min=xpos
  250.                         if ypos<y_min then
  251.                                 y_min=ypos
  252.                         if xpos>x_max then
  253.                                 x_max=xpos
  254.                         if ypos>y_max then
  255.                                 y_max=ypos
  256.                 end; else; do
  257.                         if ~PlotPixel(xpos-x_min,ypos-y_min) then
  258.                                 return '#!'
  259.                 end;
  260.                 line=ReadLine(file)
  261.         end
  262.  
  263. return line
  264.  
  265. /*************************************************
  266.  ** ParseRLE                                    **
  267.  ** RunLengthEncoded                            **
  268.  *************************************************/
  269. ParseRunLength: Procedure       expose x_min y_min x_max y_max pass xoffset yoffset
  270.         parse arg file,line
  271.  
  272.         line=substr(line,3)     /* Get extra coordinates */
  273.  
  274.         xo=word(line,1)
  275.         yo=word(line,2)    /* Get offsets, if present */
  276.  
  277.         if xo='' then
  278.                 xo=0
  279.  
  280.         if yo='' then
  281.                 yo=0
  282.  
  283.         if (~datatype(xo,'W') | ~datatype(yo,'W')) then; do
  284.                 Call PostError('Found invalid offsets in RunLengthEncoding.')
  285.                 return '#!';
  286.         end;
  287.  
  288.         line=ReadLine(file)
  289.         if line~='#^' then; do
  290.                 chck=compress(line,' ')
  291.                 if left(chck,2)='x=' then
  292.                         ps=index(chck,',')
  293.                         if ps>0 then
  294.                                 if substr(chck,ps+1,2)='y=' then
  295.                                         line=ReadLine(file)
  296.                 /* Ignore size */
  297.                 return ParseRunLengthLine(file,line,xo+xoffset,yo+yoffset)
  298.         end
  299.  
  300. return line
  301.  
  302.  
  303. /*************************************************
  304.  ** ParsePicture                                **
  305.  ** parse a picture command (w. given offset)   **
  306.  *************************************************/
  307. ParsePicture:   Procedure       expose x_min y_min x_max y_max pass xoffset yoffset
  308.         parse arg file,line
  309.  
  310.         line=substr(line,3)     /* Get extra coordinates */
  311.  
  312.         xo=word(line,1)
  313.         yo=word(line,2)    /* Get offsets, if present */
  314.  
  315.         if xo='' then
  316.                 xo=0
  317.  
  318.         if yo='' then
  319.                 yo=0
  320.  
  321.         if (~datatype(xo,'W') | ~datatype(yo,'W')) then; do
  322.                 Call PostError('Found invalid offsets in picture.')
  323.                 return '#!';
  324.         end;
  325.  
  326.         re=ParsePictureLine(file,ReadLine(file),xo+xoffset,yo+yoffset)
  327.  
  328. return re
  329.  
  330. /*************************************************
  331.  ** ParseList                                   **
  332.  ** parse a list command (w. given offset)      **
  333.  *************************************************/
  334. ParseList:      Procedure       expose x_min y_min x_max y_max pass
  335.         parse arg file,line,xoffset,yoffset
  336.  
  337.         line=substr(line,3)     /* Get extra coordinates */
  338.  
  339.         xo=word(line,1)
  340.         yo=word(line,2)    /* Get offsets, if present */
  341.  
  342.         if xo='' then
  343.                 xo=0
  344.  
  345.         if yo='' then
  346.                 yo=0
  347.  
  348.         if (~datatype(xoffset,'W') | ~datatype(yoffset,'W')) then; do
  349.                 Call PostError('Found invalid offsets in list.')
  350.                 return '#!';
  351.         end;
  352.  
  353.         re=ParseListLine(file,ReadLine(file),xo+xoffset,yo+yoffset)
  354.  
  355. return re
  356.  
  357. /*************************************************
  358.  ** ParseBlock                                  **
  359.  ** ignore a block                              **
  360.  ** (not yet supported)                         **
  361.  *************************************************/
  362. ParseBlock:     Procedure
  363.         parse arg file,line
  364.  
  365.         line=ReadLine(file)
  366.         do while left(line,1)~='#'       /* until new command */
  367.                 line=ReadLine(file)
  368.         end
  369.  
  370. return line
  371.  
  372. /*************************************************
  373.  ** ParseFile                                   **
  374.  ** Parse the given file, pass numbers the pass **
  375.  ** (first pass gets dimension)                 **
  376.  *************************************************/
  377. ParseFile:      Procedure       expose x_min y_min x_max y_max filecount
  378.         parse arg name,pass,xoffset,yoffset
  379.  
  380.         format=0                /* Default to old XLife */
  381.         lf=d2c(10)
  382.         sq=d2c(39)
  383.         filecount=filecount+1   /* Construct a unique name */
  384.         if ~open(filecount,name,'R') then; do
  385.                 Call PostError('Error while opening source' || lf || name || ' .')
  386.                 return 0
  387.         end
  388.  
  389.  
  390.         line=ReadLine(filecount)
  391.         do until eof(filecount)
  392.                 if left(line,1)='#' then; do
  393.                         format=1        /* It is now XLife 2 ! */
  394.                         cmd=substr(line,2,1)
  395.                         select          /* Quiet a lot of them are simply ingored */
  396.                                 when cmd='R' then
  397.                                         line=ParseList(filecount,line,0,0)
  398.                                 when cmd='A' then
  399.                                         line=ParseList(filecount,line,xoffset,yoffset)
  400.                                 when cmd='P' then
  401.                                         line=ParsePicture(filecount,line)
  402.                                 when cmd='I' then
  403.                                         line=ParseInclude(line,name)
  404.                                 when cmd='E' then
  405.                                         line=ParseRunLength(filecount,line)
  406.                                 when cmd='B' then
  407.                                         line=ParseBlock(filecount,line)
  408.                                 when cmd='!' then;do    /* Error condition generated by code above */
  409.                                         Call close(filecount)
  410.                                         return 0
  411.                                 end
  412.                                 when cmd='^' then;do    /* End of file condition generated by line parser */
  413.                                         Call close(filecount)
  414.                                         return 1
  415.                                 end
  416.                                 otherwise
  417.                                         line=ReadLine(filecount)
  418.                         end
  419.                 end; else; do
  420.                         /* Oops! We got something unexpected ! Let's see what we can make out of it !*/
  421.                         if format=1 then; do
  422.                                 Call PostError('Found unexpected line while parsing' || lf || name || ' .')
  423.                                 Call close(filecount)
  424.                                 return 0
  425.                         end
  426.                         select
  427.                                 when verify(line,'0123456789 +-')=0 then
  428.                                         line=ParseListLine(filecount,line,0,0)
  429.                                 when verify(line,'.* +-')=0 then
  430.                                         line=ParsePictureLine(filecount,line,0,0)
  431.                                 otherwise; do
  432.                                         Call PostError('Can' || sq || 't identify a line while parsing' || lf || name || ' .')
  433.                                         Call close(filecount)
  434.                                         return 0
  435.                                 end
  436.                         end
  437.                 end
  438.         end
  439.  
  440.         Call close(filecount)
  441. return 1
  442.  
  443.  
  444. /*************************************************
  445.  ** ReOpen                                      **
  446.  ** Open given file                             **
  447.  *************************************************/
  448. ReOpen:
  449.  
  450.  
  451. return 1
  452.  
  453. /*************************************************
  454.  ** ImportXLife(filename)                       **
  455.  ** main procedure                              **
  456.  *************************************************/
  457. ImportXLife:   Procedure
  458.    parse arg name
  459.    lf=d2c(10)
  460.  
  461.    x_min=65536
  462.    y_min=65536
  463.    x_max=-x_min
  464.    y_max=-y_min
  465.    filecount=0
  466.  
  467.    if ~ParseFile(name,0,0,0) then
  468.         return 0
  469.  
  470.    if x_max<x_min | y_max<y_min then; do
  471.         Call PostError('The file contains no data.')
  472.         return 0
  473.    end
  474.  
  475.    xof=-x_min
  476.    yof=-y_min
  477.    width=x_max-x_min+1
  478.    height=y_max-y_min+1
  479.  
  480.    'GETBORDER' 'border'
  481.    if width>border.xmax+1 | height>border.ymax+1 then; do
  482.       Call PostError('The brush is too big.' || lf || 'I need at least' width || 'x' || height 'pixels.')
  483.       return 0
  484.    end
  485.  
  486.    'PUSHTOOL' 'CELL_WINDOW' 'DRAW_DOTS' 'LEFT' 'LEFT'
  487.    'PUSHTOOL' 'CELL_WINDOW' 'DOTS' 'LEFT' 'LEFT'
  488.    'PUSHTOOL' 'CELL_WINDOW' 'SWAP' 'LEFT' 'LEFT'
  489.    'LOCKUNDO'
  490.    'FETCHUNDO'
  491.    'PUSHTOOL' 'CELL_WINDOW' 'CLEAR' 'LEFT' 'LEFT'
  492.  
  493.    if ~ParseFile(name,1,0,0) then
  494.         return 0
  495.  
  496.    'PUSHTOOL' 'CELL_WINDOW' 'CUT' 'LEFT' 'LEFT'
  497.    'DRAG' 0 0 width-1 height-1 'LEFT'
  498.    'PUSHTOOL' 'CELL_WINDOW' 'UNDO' 'LEFT' 'LEFT'
  499.    'PUSHTOOL' 'CELL_WINDOW' 'SWAP' 'LEFT' 'LEFT'
  500.  
  501. return 1
  502.  
  503.